<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="QQ账号" prop="qqAccount">
        <el-input
          v-model="queryParams.qqAccount"
          placeholder="请输入QQ账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="网名" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入网名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原绑定手机号" prop="originalPhone">
        <el-input
          v-model="queryParams.originalPhone"
          placeholder="请输入原绑定手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="现绑定手机号" prop="currentPhone">
        <el-input
          v-model="queryParams.currentPhone"
          placeholder="请输入现绑定手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入使用人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机型号" prop="phoneModel">
        <el-input
          v-model="queryParams.phoneModel"
          placeholder="请输入手机型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门" prop="department">
        <el-input
          v-model="queryParams.department"
          placeholder="请输入部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核查时间" prop="checkTime">
        <el-date-picker clearable
          v-model="queryParams.checkTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择核查时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['quick:account:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['quick:account:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['quick:account:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['quick:account:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="accountList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="QQ账号" align="center" prop="qqAccount" />
      <el-table-column label="密码" align="center" prop="password" />
      <el-table-column label="网名" align="center" prop="nickname" />
      <el-table-column label="原绑定手机号" align="center" prop="originalPhone" />
      <el-table-column label="现绑定手机号" align="center" prop="currentPhone" />
      <el-table-column label="使用人" align="center" prop="userName" />
      <el-table-column label="手机型号" align="center" prop="phoneModel" />
      <el-table-column label="部门" align="center" prop="department" />
      <el-table-column label="状态" align="center" prop="status" width="120">
        <template slot-scope="scope">
          <div style="display: flex; align-items: center; justify-content: center; gap: 8px;">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="handleStatusChange(scope.row)"
              v-hasPermi="['quick:account:edit']">
            </el-switch>
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'" size="mini">
              {{ scope.row.status === 1 ? '正常' : '停用' }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="核查时间" align="center" prop="checkTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['quick:account:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['quick:account:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改QQ账号管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body class="account-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <!-- 账号信息区域 -->
        <div class="form-section">
          <div class="section-title">
            <i class="el-icon-user" style="color: #409EFF;"></i>
            <span>账号信息</span>
          </div>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="QQ账号" prop="qqAccount">
                <el-input v-model="form.qqAccount" placeholder="请输入QQ账号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="密码" prop="password">
                <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="网名" prop="nickname">
                <el-input v-model="form.nickname" placeholder="请输入网名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态" prop="status">
                <el-switch
                  v-model="form.status"
                  :active-value="1"
                  :inactive-value="0"
                  active-text="正常"
                  inactive-text="停用">
                </el-switch>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <!-- 手机信息区域 -->
        <div class="form-section">
          <div class="section-title">
            <i class="el-icon-mobile-phone" style="color: #67C23A;"></i>
            <span>手机信息</span>
          </div>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="原绑定手机号" prop="originalPhone">
                <el-input v-model="form.originalPhone" placeholder="请输入原绑定手机号" maxlength="11" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="现绑定手机号" prop="currentPhone">
                <el-input v-model="form.currentPhone" placeholder="请输入现绑定手机号" maxlength="11" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="手机型号" prop="phoneModel">
                <el-input v-model="form.phoneModel" placeholder="请输入手机型号" />
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <!-- 使用信息区域 -->
        <div class="form-section">
          <div class="section-title">
            <i class="el-icon-s-custom" style="color: #E6A23C;"></i>
            <span>使用信息</span>
          </div>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="使用人" prop="userName">
                <el-input v-model="form.userName" placeholder="请输入使用人" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="部门" prop="department">
                <el-input v-model="form.department" placeholder="请输入部门" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="核查时间" prop="checkTime">
                <el-date-picker clearable
                  v-model="form.checkTime"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择核查时间"
                  style="width: 100%">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <!-- 备注区域 -->
        <div class="form-section">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入备注信息" />
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAccount, getAccount, delAccount, addAccount, updateAccount } from "@/api/quick/account"

export default {
  name: "Account",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // QQ账号管理表格数据
      accountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        qqAccount: null,
        password: null,
        nickname: null,
        originalPhone: null,
        currentPhone: null,
        userName: null,
        phoneModel: null,
        department: null,
        status: null,
        checkTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        qqAccount: [
          { required: true, message: "QQ账号不能为空", trigger: "blur" }
        ],
        originalPhone: [
          { pattern: /^1[3-9]\d{9}$/, message: "请输入正确的手机号码", trigger: "blur" }
        ],
        currentPhone: [
          { pattern: /^1[3-9]\d{9}$/, message: "请输入正确的手机号码", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询QQ账号管理列表 */
    getList() {
      this.loading = true
      listAccount(this.queryParams).then(response => {
        this.accountList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        qqAccount: null,
        password: null,
        nickname: null,
        originalPhone: null,
        currentPhone: null,
        userName: null,
        phoneModel: null,
        department: null,
        status: 1,  // 默认正常（数字类型）
        checkTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加QQ账号管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getAccount(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改QQ账号管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAccount(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addAccount(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除QQ账号管理编号为"' + ids + '"的数据项？').then(function() {
        return delAccount(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('quick/account/export', {
        ...this.queryParams
      }, `account_${new Date().getTime()}.xlsx`)
    },
    /** 状态切换 */
    handleStatusChange(row) {
      const statusText = row.status === 1 ? '正常' : '停用'
      const text = `确认要将"${row.qqAccount}"的状态修改为"${statusText}"吗？`
      this.$modal.confirm(text).then(() => {
        const updateData = {
          id: row.id,
          status: row.status
        }
        updateAccount(updateData).then(response => {
          this.$modal.msgSuccess("状态修改成功")
          this.getList()
        }).catch(() => {
          // 失败时恢复原状态
          row.status = row.status === 1 ? 0 : 1
        })
      }).catch(() => {
        // 取消时恢复原状态
        row.status = row.status === 1 ? 0 : 1
      })
    }
  }
}
</script>

<style scoped>
/* 对话框样式 */
.account-dialog .el-dialog__body {
  padding: 10px 20px;
}

/* 表单区域样式 */
.form-section {
  margin-bottom: 20px;
  padding: 15px;
  border: 1px solid #EBEEF5;
  border-radius: 6px;
  background-color: #FAFAFA;
}

.form-section:last-child {
  margin-bottom: 0;
}

/* 区域标题样式 */
.section-title {
  margin-bottom: 15px;
  padding-bottom: 8px;
  border-bottom: 2px solid #E4E7ED;
  font-size: 14px;
  font-weight: bold;
  color: #303133;
}

.section-title i {
  margin-right: 6px;
  font-size: 16px;
}

/* 表单项间距调整 */
.form-section .el-form-item {
  margin-bottom: 15px;
}

.form-section .el-form-item:last-child {
  margin-bottom: 0;
}

/* 对话框底部按钮样式 */
.dialog-footer {
  text-align: center;
  padding: 15px 0 0 0;
}

.dialog-footer .el-button {
  min-width: 100px;
  margin: 0 10px;
}
</style>
